package memoryimage.provider;

import memoryimage.models.ByteDataArray;
import memoryimage.models.ByteDataArrayListener;
import memoryimage.models.MemoryImage;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;

public class MemoryContentProvider implements IStructuredContentProvider,
	ByteDataArrayListener {
	TableViewer viewer;
	
	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return ((MemoryImage) inputElement).getList().toArray();
	}

	public void dispose() {
		// TODO Auto-generated method stub	
	}

	public void addByteDataArray(ByteDataArray byteDataArray){
		viewer.add(byteDataArray);
	}
	
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
		this.viewer = (TableViewer) viewer;
		
		if(oldInput != null) {
			((MemoryImage)oldInput).removeListener(this);
		}
		
		if(newInput != null) {
			((MemoryImage)newInput).addByteDataArrayListener(this);
		}
	}

	public void add(ByteDataArray byteDataArray) {
		// TODO Auto-generated method stub
		if (viewer != null) {
			viewer.add(byteDataArray);
		}
	}

	public void remove(ByteDataArray byteDataArray) {
		// TODO Auto-generated method stub
		if (viewer != null) {
			viewer.remove(byteDataArray);
		}
	}

}
